package com.dream.master.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dream.app.cache.AppOrgCache;
import com.dream.app.service.OrgService;
import com.dream.dao.master.ShopDAO;
import com.dream.dao.master.ShopUserDAO;
import com.dream.master.service.ShopService;
import com.dream.model.app.AppAbstractUser;
import com.dream.model.app.AppOrg;
import com.dream.model.app.AppPosition.PositionType;
import com.dream.model.master.Shop;
import com.dream.model.master.ShopUser;
import com.dream.utils.collection.CollectionHelper;
import com.dream.utils.hibernate.PagingVO;
import com.dream.vo.master.ShopVO;
import com.google.common.collect.Lists;

@Service("ShopService")
public class ShopServiceImpl implements ShopService {
	
	@Autowired
	private AppOrgCache orgCache;
	
	@Autowired
	private OrgService orgService;
	
	@Autowired
	private ShopUserDAO shopUserDAO;
	
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Override
	@Transactional
	public PagingVO query(boolean scope,ShopVO vo, PagingVO page,AppAbstractUser user) {
		
		populateCriteria(scope,vo,user);
		return queryShop(vo,page,user);
	}
	
	@Transactional
	private void setOrg(Shop shop){
		if(shop != null){
			List<AppOrg> orgs = orgService.getParent(shop.getOrgId());
			shop.initOrgs(orgs);
		}
		
	}
	
	@Transactional
	private void populateCriteria(boolean scope,ShopVO vo,AppAbstractUser user){
		
		if(!scope){
			
			List<Long> leafOrgs = orgService.getLeafOrgs(vo.getFirstOrg(), vo.getSecondOrg());
			vo.setOrgs(leafOrgs);
			return;
		}
		
		if(user.getPosition() == null) {return ;}
		
		if(PositionType.staff.equals(user.getPosition().getType())){
			
			List<ShopUser> list = shopUserDAO.search(user.getId(),null);
			Collection<String> shops = CollectionHelper.colToCol(list, new CollectionHelper.Process<ShopUser,String>(){
				@Override
				public String doProcess(ShopUser t) {
					return t.getShopId();
				}
			});
			vo.setShops(shops);
		}
		
		if(PositionType.manager.equals(user.getPosition().getType())){
			
			List<Long> leafOrgs = orgService.getLeafOrgs(vo.getFirstOrg(), vo.getSecondOrg());
			vo.setOrgs(leafOrgs);
			
		}
		
	}
	

	@Override
	@Transactional
	public Shop queryById(String id) {
		if(id == null) return null;
		Shop shop = shopDAO.selectById(id);
		if(shop != null){
			List<AppOrg> orgs = orgService.getParent(shop.getOrgId());
			shop.initOrgs(orgs);
		}
		return shop;
	}

	@Override
	@Transactional
	public void update(ShopVO vo, AppAbstractUser user) {
		
		Shop shop = new Shop();
		if(StringUtils.isNotBlank(vo.getId())){
			shop = shopDAO.selectById(vo.getId());
			shop.setUpdateTime(new Date());
			shop.setUpdateUser(user.getId());
		}else{

			shop.setCreateTime(new Date());
			shop.setCreateUser(user.getId());
		}
		vo.populate(shop);
		
		shopDAO.saveOrUpdate(shop);

	}

	@Override
	@Transactional
	public void delete(String id, AppAbstractUser user) {
		
		Shop shop = shopDAO.selectById(id);
		shop.setDeleted(true);
		shopDAO.saveOrUpdate(shop);

	}
	@Override
	@Transactional
	public PagingVO queryShopUser(ShopVO vo, PagingVO page, AppAbstractUser user) {
		
		return shopUserDAO.query(vo, page);
	}
	
	@Override
	@Transactional
	public void addShopUsers(String shopId, String[] userId, AppAbstractUser user) {
		
		if(shopId != null && ArrayUtils.isNotEmpty(userId)){
			List<ShopUser> suList = Lists.newArrayList();
			Date date = new Date();
			for(String uid : userId){
				ShopUser shopUser = new ShopUser();
				shopUser.setUserId(uid);
				shopUser.setShopId(shopId);
				shopUser.setCreateTime(date);
				shopUser.setCreateUser(user.getId());
				suList.add(shopUser);
			}
			shopUserDAO.saveOrUpdateBatch(suList);
			
		}
	}
	@Override
	@Transactional
	public void deleteShopUser(Long id, AppAbstractUser user) {
		
		ShopUser shopUser = shopUserDAO.selectById(id);
		shopUser.setDeleted(true);
		shopUserDAO.saveOrUpdate(shopUser);
		
	}
	@Override
	@Transactional
	public List<Shop> queryMyShop(AppAbstractUser user) {
		
		List<ShopUser> list = shopUserDAO.search(user.getId(), null);
		List<Shop> shops = CollectionHelper.colToList(list, new CollectionHelper.Process<ShopUser,Shop>(){
			@Override
			public Shop doProcess(ShopUser t) {
				return t.getShop();
			}
		});
		return shops;
	}
	
	@Override
	@Transactional
	public List<String> queryMyShopId(AppAbstractUser user) {
		
		List<ShopUser> list = shopUserDAO.searchMyShop(user.getId());
		List<String> shops = CollectionHelper.colToList(list, new CollectionHelper.Process<ShopUser,String>(){
			@Override
			public String doProcess(ShopUser t) {
				return t.getShopId();
			}
		});
		return shops;
	}
	
	@Override
	@Transactional
	public PagingVO queryShop(ShopVO vo, PagingVO page,AppAbstractUser user) {
		PagingVO pageVO = shopDAO.query(vo, page);
		@SuppressWarnings("unchecked")
		List<Shop> li = (List<Shop>) pageVO.getDetails();
		if(CollectionUtils.isNotEmpty(li)){
			for(Shop item : li){
				setOrg(item);
			}
		}
		
		return pageVO;
	}
	

}
